<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Auction
 * @version 	: 1.1
 * COPYRIGHT    : 2010
 * ------------------------------------------------------
 *
 * Created on	:  Sep 13, 2010
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls('controllers.AbstractBackEndController');

/**
 * The controller for State application
 *
 */
class DownloadFrontController extends AbstractBackEndController {

	var $_model;

	function __construct() {
		parent::__construct();
		SysApplication::importModelCls('DownloadModel');
		$this->_model = new DownloadModel();
	}
	function display() {
		$lang = $_REQUEST['lang'];
		$model = $this->_model;
		$root = SYS_ROOT_URL;
		$result = $model->loadFiles();
		$photo = SysLanguage::_("PHOTO");
		$video = SysLanguage::_("VIDEO");
		$presentation = SysLanguage::_("PRESENTATION");

		//Load title------------
		$sql = "SELECT * FROM title WHERE id=5";
		$resultTitle = SysDatabase::loadObject($sql);
		if($_REQUEST['lang'] == 'fr'){
			$title = $resultTitle->title_fr;
		} else if($_REQUEST['lang'] == 'en'){
			$title = $resultTitle->title_en;
		}
		//$title = SysLanguage::_('TITLE');
		$download = "";
		$session = SysAuthenticate::getAuthenticateObject();
		$sess_username = $session->username;

		foreach ($result as $row){
			$id = $row->download_id;
				
			$link = $row->file;
			//$down = SysLanguage::_('DOWNLOAD');
			$image = $row->image;
			if($_REQUEST['lang'] == 'fr'){
				$content = $row->des_fr;
			} else if($_REQUEST['lang'] == 'en'){
				$content = $row->des_en;
			}
			
				
			if(!empty($sess_username)){
				$link_dowload = $root."download/downloadFiles/".$id;
			} else {
				$link_dowload = $root.$lang."/authenticate";
			}
			
			if (empty($link)){
				$display = "display:none";
			}else {
				$display = "";
			}
				
			eval('$download .= "'.SysApplication::loadTemplate('front/download_item').'";');
		}
		eval('echo "'.SysApplication::loadTemplate('front/default').'";');
	}
	/*function loadFileType(){
		$root_path = SYS_ROOT_URL;
		$root_dir = SYS_ROOT_PATH;
		$model = $this->_model;
		$result = $model->loadFileType();
		foreach ($result as $row){
		$id = $row->id;
		$name = $row->file_name;
		$link = $row->url;
		$down = SysLanguage::_('DOWNLOAD');
		$downloadId = $row->id;
		if($_REQUEST['lang'] == 'fr'){
		$content = $row->content_vi;
		$file_type_name = $row->file_type_name_vi;
		} else if($_REQUEST['lang'] == 'en'){
		$content = $row->content_en;
		$file_type_name = $row->file_type_name_en;
		}

		eval('$download .= "'.SysApplication::loadTemplate('front/download_item').'";');
		}
		eval('echo "'.SysApplication::loadTemplate('front/default').'";');
		}*/
	function downloadFiles(){
		$root = SYS_ROOT_URL;
		$root_dir = SYS_ROOT_PATH;
		$a = SysRequest::getVar('task');
		$sql = "SELECT * FROM download WHERE download_id = '{$a}'";
		$result = SysDatabase::loadObject($sql);
		$link = $result->file;
		/*   Download files  */
		$url = $root_path.$link;
		$dir = $root_dir.$link;
		$loadName = explode('/', $link);
		$file_name = $loadName[2];
		$fp = fopen($dir, "rb");
		header("Content-Transfer-Encoding: binary");
		header("Content-length: " . filesize($dir));
		header("Content-type: application/octet-stream");
		header("Content-Disposition: attachment; filename=\"$file_name\"");
		fpassthru($fp);
		fclose($fp);
		die();
		eval('echo "'.SysApplication::loadTemplate('front/default').'";');
	}

}
